<?php
/**
 * TopupController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';

class User_TopupController extends Zend_Controller_Action {
	public function preDispatch() {
		if (! isset($_SESSION)) {
			session_start ();
		}
		if (! isset ( $_SESSION ["normal_user"] )) {
			$this->_redirect ( '/index' );
		}
	}
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$user_model = new Application_Model_User;
		$user = $user_model->getUserById($_SESSION ["normal_user"]->id);
		$this->view->paid_token = $user->paid_token ? $user->paid_token : 0;
		$this->view->free_token = $user->free_token ? $user->free_token : 0;
	}
	
	public function topupAction() {
	
	}
	
	public function historyAction() {
		$topup_model = new Application_Model_Topup;
		$this->view->topups = $topup_model->fetchAll(array('user_id'=>$_SESSION ["normal_user"]->id));
	}
	
	public function alipayAction() {
		include_once ("payment/alipay/alipaydirect/trade/alipayto.php");
	}
	
	public function alipayreturnAction() {
		//关闭layout 和 view(不需要phtml)
		Zend_Layout::getMvcInstance()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		//include_once ("payment/alipay/alipaydirect/trade/return_url.php");
		
		$pay_success = true; //测试
		if($pay_success) {
			$topup_model = new Application_Model_Topup;
			$user_model = new Application_Model_User;
			
//			$topup = array();
//			$topup['user_id'] = $_SESSION ["normal_user"]->id;
//			$topup['order_num'] = $out_trade_no; //订单号
//			$topup['trade_num'] = $trade_no;     //支付宝交易号
//			$topup['token_type'] = Application_Model_Constants::$PAID_TOKEN;
//			$topup['topup_time'] = time();
//			$topup['amount'] = intval($total_fee) * 100;
//			$topup['pay_method'] = "支付宝";

			//测试
			//			$topup = array();
			$topup['user_id'] = $_SESSION ["normal_user"]->id;
			$topup['order_num'] = date('Ymdhis'); //订单号
			$topup['trade_num'] = date('Ymdhis');     //支付宝交易号
			$topup['token_type'] = Application_Model_Constants::$PAID_TOKEN;
			$topup['topup_time'] = time();
			$topup['amount'] = $_POST['total_fee'] * 100;
			$topup['pay_method'] = "支付宝";
			
			if($topup_model->getTopupByTradeNo($topup['order_num'])) {
				$this->_redirect('/error');
			}

			try {
				$insert_id = $topup_model->insert($topup);
				$topup = $topup_model->getTopupById($insert_id);
				$updateSuccess = $user_model->addPaidToken($topup->user_id, $topup->amount);
				if(! $updateSuccess) {
					//跳转到充值失败页面
					$this->_redirect('/error');
				} else {
					$this->_redirect('/user/topup');
				}				
			} catch (Exception $e) {
				//跳转到充值失败页面
				$this->_redirect('/error');
			}
		} else {
			//跳转到充值失败页面
			$this->_redirect('/error');
		}
	}
	
	public function alipaynotifyAction() {
		//关闭layout 和 view(不需要phtml)
		Zend_Layout::getMvcInstance()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		include_once ("payment/alipay/alipaydirect/trade/notify_url.php");
	}
}
